/*       I am just a fish swimming on the surface of the water, and you happened to discover me      */         
/* Includes ------------------------------------------------------------------*/
#include "app.h"
#include "FreeRTOS.h"
#include "task.h"
#include "G_Led.h"
#include "G_RTC.h"
#include "G_USART.h"
#include "GSCSD.h"
#include "G_W25Qxx.h"
#include "G_AS608.h"
#include "G_AS608Demo.h"
#include "Gkey_board.h"
#include "Gkey_board_sample.h"
#include "keypad.h"
#include "esp8266.h"
#include "syn6288.h"
#include "rb.h"
#include "kaoqinApp.h"
/* Private define-------------------------------------------------------------*/

/* Private variabl es----------------------------------------------------------*/
TaskHandle_t User1Task_Handler; //任务句柄
TaskHandle_t MAINTask_Handler; 
TaskHandle_t KaoqinTask_Handler; 

espTime* rtcTime;

taskParam TaskParam;
/* Public variables-----------------------------------------------------------*/

extern kaoqinApp kaoqinapp;
/* Private function prototypes------------------------------------------------*/      
void G_TASKUSER1(void *pvParameters);
void G_TASKMAIN(void *pvParameters);
uint32_t DeviceID;
uint32_t FlashID;
/**
  * @brief  系统初始化
  * @param  None
  * @retval None
  */
void appInit(void){
/***********************************************设备外设初始化************************************/
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);//设置系统中断优先级分组 
	USART_Config();
	USART3Init(115200);

	/* RTC配置：选择时钟源，设置RTC_CLK的分频系数 */
	RTC_CLK_Config();

	
/***********************************************设备底层初始化************************************/	
  /* led初始化 */
	ledGpioConfig();
	/* 按键初始化 */
	GPIO_Key_Board_Init();
	/* 4X4薄膜按键初始化 */
	KEYPAD_Init();	
	/* wifi模块初始化 */
	WIFI_Init(115200);
	/* 语音模块初始化 */
	USART6_init(9600);
	/* 环形缓冲区初始化 */
	RingBuff_Init();

	/* 16M串行flash W25Q128初始化 */
	SPI_FLASH_Init();
	
	/* 获取 Flash Device ID */
	DeviceID = SPI_FLASH_ReadDeviceID();
	
	delay_ms( 1000 );
	
	/* 获取 SPI Flash ID */
	FlashID = SPI_FLASH_ReadID();
	
	UsartPrintf(USART1,"\r\nFlashID is 0x%x,  Manufacturer Device ID is 0x%x\r\n", FlashID, DeviceID);

/***********************************************设备应用层初始化************************************/

/***********************************************应用程序初始化************************************/
	UsartPrintf(USART1,"USART1 IS OK\r\n");
	
	//栈总大小不要超过10k
	xTaskCreate(G_TASKMAIN,"TASKMAIN",2*1024,(void *)&TaskParam,1,&MAINTask_Handler);
	xTaskCreate(G_TASKUSER1,"TASKUSER1",512,NULL,5,&User1Task_Handler);
	xTaskCreate(G_kaoqinRun,"kaoqinApp",5*1024,(void *)&TaskParam,1,&KaoqinTask_Handler);

	vTaskStartScheduler(); //开启任务调度
}

/**
	* @brief  系统运行（此处禁止写任何程序）
  * @param  None
  * @retval None
  */
void appRun(void){
	 
}



/**
  * @brief  主要任务逻辑处理
  * @param  None
  * @retval None
  */


#define Time_GET "GET"
void G_TASKMAIN(void *pvParameters){
   
	
	taskParam *paramPtr = (taskParam *)pvParameters;
	//写入开机次数
	 SaveParam *ptr = NULL;
	 ptr = ReadToParam(); 
	 if(ptr->PowerCnt!=0xffff){
		 UsartPrintf(USART1,"powerCNT:%d\r\n",ptr->PowerCnt);
		 ptr->PowerCnt+=1;
		 WriteToParam();
	 }
	 while(GAS608Init(&AS608Addr))//与AS608模块握手
	{
		UsartPrintf(USART1,"Cannot connect with AS608!\r\n");
		delay_ms(1000);
		UsartPrintf(USART1,"Try to connect again....\r\n");
		delay_ms(1000);	  
	}
	UsartPrintf(USART1,"AS608 IS OK\r\n");
	u16 ValidN;
	u8 ensure=PS_ValidTempleteNum(&ValidN);//读库指纹个数
	if(ensure!=0x00)
		ShowErrMessage(ensure);//显示确认码错误信息	
	
	ensure=PS_ReadSysPara(&AS608Para);  //读AS608模块参数 
	
	if(ensure==0x00)
	{
		UsartPrintf(USART1,"RemainNum:%d    Level:%d",AS608Para.PS_max-ValidN,AS608Para.PS_level);//显示剩余指纹数量和安全等级
	}
	else
		ShowErrMessage(ensure);
	 	/* WIFI初始化 */
		WIFI_Init(115200);
		ESP8266_Init();
	  paramPtr->initstate=1;
		while(1){
			
		 if(xTaskGetTickCount()%1000==0){  //1s进一次   时间任务
			  uint8_t *ptr = (uint8_t *)GetDataTime();
				UsartPrintf(USART1,"%04d/%02d/%02d\r\n",ptr[0]+2000,ptr[1],ptr[2]);
			  UsartPrintf(USART1,"%02d:%02d:%02d\r\n\r\n",ptr[3],ptr[4],ptr[5]);
		 }
		 
			if(key_check_state(KEY1,KEY_PRESS)){
				rtcTime = getEspTime();
				setDate(rtcTime->year,rtcTime->month,rtcTime->day,0);
				setTime(rtcTime->hour,rtcTime->minute,rtcTime->second);
			  ledTog(2);
			}
		 
			AS608Run(0);   //指纹模块运行函数
			vTaskDelay(1);

		}
}
/**
* @brief  实时响应任务 优先级应该为最高
  * @param  None
  * @retval None
  */
void G_TASKUSER1(void *pvParameters){

		while(1){
			if(xTaskGetTickCount()%1000==0){  //1s进一次
				ledTog(1);
				rtc_run();
			}
			key_check();    //独立按键扫描函数
			vTaskDelay(1);
		}
}




/********************************************************
  End Of File
********************************************************/
